package com.hs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.entity.Consultant;
import com.hs.entity.User;
import com.hs.mapper.ConsultantMapper;
import com.hs.mapper.UserMapper;
import com.hs.service.ConsultantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName ConsultantServiceImpl
 * @Description TODO
 * @Author qiuye
 * @Date 2025年08月01日 19:31
 * @Version 1.0
 */
@Service
public class ConsultantServiceImpl implements ConsultantService {
    @Autowired
    private ConsultantMapper consultantMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public int addConsultant(Consultant consultant) {
        return consultantMapper.addConsultant(consultant);
    }

    @Override
    public int updateConsultant(Consultant consultant) {
        return consultantMapper.updateConsultant(consultant);
    }

    @Override
    public int deleteConsultant(int id) {
        // 先根据id来查询其关联的外键是有有值，如果有数据，则不可删除；反之可以删除

        return consultantMapper.deleteConsultant(id);
    }

    @Override
    public int deleteConsultantIds(int[] ids) {
        return consultantMapper.deleteConsultantIds(ids);
    }

    @Override
    public Consultant getConsultant(int id) {
        Consultant consultant = consultantMapper.getConsultant(id);
        return consultant;
    }

    @Override
    public PageInfo<Consultant> getConsultants(Consultant consultant, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Consultant> list = consultantMapper.getConsultants(consultant);
        PageInfo<Consultant> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int sc(int uid, int cid) {
        // 把uid和cid增加到收藏表中
        // consultantMapper.add(uid,cid);
        // 收藏后，需要给当前的用户增加积分
        User user = userMapper.findUserById(uid);
        int points = user.getPoints()+10;
        userMapper.updatePointsById(uid,points);
        return 0;
    }
}
